/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Chat.java
 *
 * Created on 16-09-2012, 05:17:52 AM
 */
package com.ssdd.chaskinetclient;

import com.ssdd.utilities.ConectionManager;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JList;
import javax.swing.JTextArea;

/**
 *
 * @author Administrador
 */
public class Chat extends javax.swing.JFrame {

    /** Creates new form Chat */
    public Chat() {
	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        chatMessage = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        message = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        userList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        chatMessage.setColumns(20);
        chatMessage.setEditable(false);
        chatMessage.setRows(5);
        chatMessage.setName("chatMessage"); // NOI18N
        jScrollPane1.setViewportView(chatMessage);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        message.setColumns(20);
        message.setRows(5);
        message.setName("message"); // NOI18N
        jScrollPane2.setViewportView(message);

        jButton1.setText("Enviar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        userList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        userList.setName("userList"); // NOI18N
        jScrollPane3.setViewportView(userList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	if (!disable) {
	    String msg = ConectionManager.getInstance().getUser() + " dice: " + this.message.getText();
	    this.chatMessage.setText(this.chatMessage.getText() + "\n" + msg);
	    this.message.setText(null);
	    List<String> userListTmp = null;
	    if (userList.getModel().getSize() > 0) {
		userListTmp = new ArrayList<String>();
		for (int i = 0; i < userList.getModel().getSize(); i++) {
		    userListTmp.add((String) userList.getModel().getElementAt(i));
		}
	    }
	    ConectionManager.getInstance().sendMessage(msg, userListTmp);
	}

    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
	this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
	this.dispose();
    }//GEN-LAST:event_formWindowClosing

    public void disableChat(String user) {
	disable = true;
	this.chatMessage.setText(this.chatMessage.getText() + "\n" + "El usuario " + user + " no se encuentra disponible...");
	this.message.setEditable(false);
    }

    public void updateChatMessage(String message) {
	this.chatMessage.setText(this.chatMessage.getText() + "\n" + message);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	java.awt.EventQueue.invokeLater(new Runnable() {

	    public void run() {
		new Chat().setVisible(true);
	    }
	});
    }

    public JList getUserList() {
	return userList;
    }

    public void setUserList(JList userList) {
	this.userList = userList;
    }

    public void setUserListBasedTo(List<String> userList) {
	this.userList.setListData(userList.toArray(new String[userList.size()]));
    }

    public JTextArea getChatMessage() {
	return chatMessage;
    }

    public void setChatMessage(JTextArea chatMessage) {
	this.chatMessage = chatMessage;
    }

    public JTextArea getMessage() {
	return message;
    }

    public void setMessage(JTextArea message) {
	this.message = message;
    }

    public void setMsgInChatMessage(String message) {
	this.chatMessage.setText(this.chatMessage.getText() + "\n" + message);

    }
    private boolean disable = false;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea chatMessage;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea message;
    private javax.swing.JList userList;
    // End of variables declaration//GEN-END:variables
}
